<?php
namespace App\Admin\Controllers;

use SmallRuralDog\Admin\Controllers\AdminController;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use SmallRuralDog\Admin\Form;
use SmallRuralDog\Admin\Grid;
use SmallRuralDog\Admin\Auth\Database\Menu;
use SmallRuralDog\Admin\Components\Attrs\SelectOption;
use SmallRuralDog\Admin\Components\Form\IconChoose;
use SmallRuralDog\Admin\Components\Form\InputNumber;
use SmallRuralDog\Admin\Components\Form\Select;
use SmallRuralDog\Admin\Components\Form\Radio;
use SmallRuralDog\Admin\Components\Form\RadioGroup;
use SmallRuralDog\Admin\Components\Grid\Avatar;
use SmallRuralDog\Admin\Components\Grid\Icon;
use SmallRuralDog\Admin\Components\Grid\Tag;
use SmallRuralDog\Admin\Components\Grid\Image;
use SmallRuralDog\Admin\Grid\Actions\ActionButton;
use SmallRuralDog\Admin\Components\Widgets\Dialog;
use SmallRuralDog\Admin\Components\Widgets\BaseForm;
use SmallRuralDog\Admin\Components\Form\Input;
use SmallRuralDog\Admin\Components\Form\Upload;
use SmallRuralDog\Admin\Components\Form\Checkbox;
use SmallRuralDog\Admin\Components\Widgets\Divider;
use SmallRuralDog\Admin\Components\Form\WangEditor;
use SmallRuralDog\Admin\Components\Form\DateTimePicker;
use SmallRuralDog\Admin\Layout\Content;
use App\Models\Member;
use App\Models\ActivityEnter;

class ActivityEnterController extends AdminController
{

    protected function grid()
    {
        $grid = new Grid(new ActivityEnter());
        $grid->column('id', "ID")->width(100)->sortable();
        $grid->column('member_name', '会员')->width(200);
         $grid->column('mobile', "手机");
        $grid->column('title', "活动名称");
        // $grid->column('description', "简介");
        $grid->column('imgurl', "活动图")->width(80)->align('center')->component(Image::make()->size(50, 50)->preview());
        $grid->column('apply', "是否报名")->customValue(function ($row, $value) {
            $apply = ['1' => '是', '2' => '否'];
            return isset($apply[$value]) ? $apply[$value] : '';
        })->width(80)->component(Tag::make()->type(["是" => "info", "否" => "danger"]));
        $grid->column('score', "可得积分");
        $grid->column('start_time', "开始时间")->sortable();
        $grid->column('end_time', "结束时间");
        $grid->column('status', "状态")->customValue(function ($row, $value) {
            $sex_name = ['order' => '已报名', 'finish' => '已完成', 'cancel'=>'已作废'];
            return $sex_name[$value];
        })->width(100)->sortable()->component(Tag::make()->type(["已报名" => "default", "已完成" => "finish", '已作废'=>"info"]));

        // $grid->actionLabel(''); // 隐藏操作

        $grid->actions(function (Grid\Actions $actions){
            $actions->hideDeleteAction(); // 隐藏删除操作
            $actions->hideEditAction(); // 隐藏编辑操作
            if ( $actions->getRow()->status=='order')
            {
                $actions->add(ActionButton::make("取消")->order(3)->handler('request')->uri(route("admin.activity.enter.cancel").'/?id='.$actions->getRow()->activity_id)->message('确定要取消该用户的报名吗？'));
            }
        })->toolbars(function (Grid\Toolbars $toolbars)
        {
            $toolbars->hideCreateButton();
        });
        $grid->quickSearch('member.name');
        $grid->quickSearchPlaceholder("请输入会员名称");

        $grid->filter(function (Grid\Filter $filter) {

            $filter->equal('title', '活动名称');

        });

        return $grid;
    }

    // 订单作废
    public function cancel(Request $request)
    {
        try {
            \Admin::validatorData($request->all(), [
                'id' => 'required',
            ],[
                'id.required' => '参数错误'
            ]);

            $activity_id = $request->input("id");

            $activity = ActivityEnter::where('activity_id', $activity_id)->first();

            if (!$activity)
            {
                return \Admin::responseError('获取订单信息失败');
            }

            if ($activity->stauts =='finish')
            {
                return \Admin::responseError('用户已签到成功，无法取消报名');
            }

            $activity->status = 'cancel';
            $activity->save();

        } catch (\Exception $exception) {
            return \Admin::responseError($exception->getMessage());
        }

        return \Admin::responseMessage('报名取消成功');
    }

}
